package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.child.*;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;


public class ChildBaseData {

    private static Logger logger = LoggerFactory.getLogger(ChildBaseData.class);

    private static ConcurrentHashMap<Integer, ChildQuality> childQualityMap = new ConcurrentHashMap<>(); // 子嗣品资表
    private static ConcurrentHashMap<Integer, ChildCareer> childCareerMap = new ConcurrentHashMap<>(); // 子嗣职业表
    private static CopyOnWriteArrayList<Integer> childMarryRobotList = new CopyOnWriteArrayList<>();//子嗣联姻机器人id表


    //初始化子嗣数据表

    public static void init(List<Future> futures) {
        futures.add(initChildQuality());
        futures.add(initChildCareer());
    }

    /**
     * 初始化子嗣品资表
     */
    public static Future<Void> initChildQuality() {
        String table = "child_quality";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ChildQuality> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ChildQuality entity = new ChildQuality(result.getJsonObject(i));
                        tmp.put(entity.getQuality(), entity);
                    }
                    childQualityMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ChildQuality getChildQuality(int id) {
        ChildQuality result = childQualityMap.get(id);
        if (result == null) {
            logger.error("getChildQuality error key = " + id);
        }
        return result;
    }

    public static ConcurrentHashMap<Integer, ChildQuality> getChildQualityMap() { return childQualityMap; }


    /**
     * 初始化子嗣职业表
     */
    public static Future<Void> initChildCareer() {
        String table = "child_career";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ChildCareer> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ChildCareer entity = new ChildCareer(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    childCareerMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }
    public static ChildCareer getChildCareer(int id) {
        ChildCareer result = childCareerMap.get(id);
        if (result == null) {
            logger.error("getChildCareer error key = " + id);
        }
        return result;
    }

    public static ConcurrentHashMap<Integer, ChildCareer> getChildCareerMap() { return childCareerMap; }

    public static int randomChildCareer() {
        List<Integer> list = childCareerMap.keySet().stream().toList();
        int random = Tool.getRandom(0, list.size() - 1);
        return list.get(random);
    }
}
